<? if (! defined('BASEPATH')) exit('No direct script access allowed');
class image_manager_editor extends CI_Controller
{
    protected $_templates;
	
    function __construct()
	{
        parent::__construct();
        $this->pre_message ='';
        $this->load->model('image_model','image');
		$this->load->helper('search') ;
    }
	function index($num_page=0)
	{
        /*Begin search*/
        $type = $this->input->post('type');
       
        $current_router = $this->router->fetch_class();
        if($this->session->userdata('current_router')&&($this->session->userdata('current_router')!=$current_router)){
            $this->session->set_userdata('key_search','');
        }
        $this->session->set_userdata('current_router',$current_router);

        $data['search_bar']=searchBar(array('search_bar'=>'search_bar_file'),array('TT'=>'Title','DEC'=>'Description','EXT'=>'Ext'),'mod_file_manager/image_manager_editor/index/');
        if($type=='ajax')
        {
            
            $arr_search['key_search'] = $this->input->post('key_search');
            $arr_search['field_search'] = $this->input->post('field_search');
            $arr_search['image_category_id']=$this->input->post('category_image_id');
            
            $this->session->set_userdata('key_search',$arr_search['key_search']);
            $this->session->set_userdata('field_search',$arr_search['field_search']);
            $this->session->set_userdata('category_image_id',$arr_search['image_category_id']);
        }
        else{
            $arr_search['key_search']=$this->session->userdata('key_search');
            $arr_search['field_search']=$this->session->userdata('field_search');
            $arr_search['image_category_id']=$this->session->userdata('category_image_id');
        }
        /*End  search*/
        
	  $data['title'] = '';
	  $config['base_url'] = base_url().'mod_file_manager/image_manager_editor/index/';  
	  $data['total'] = $this->image->getNumFile($arr_search);
	  $config['total_rows']   =  $data['total'];
	  $config['per_page']  =   '10';
	  $config['uri_segment'] = 3;   
	  $this->pagination->initialize($config);   
	  $data['list'] =   $this->image->getAllFile($config['per_page'],(int)$num_page,$arr_search);
	  $data['pagination']    = $this->pagination->create_links(); 
      
      $data['list_category']=$this->image->getAllCategoryImage();
	  
      if($type=="ajax"){
            echo $this->load->view('editor/ajax_search_view',$data,true);
      }else{
            $this->_templates['page'] = 'image_manager_editor/index';
            $this->site->load($this->_templates['page'],$data,'editor');
        }
	}

	function upload()
	{
		$this->load->library('upload_library');
		$data['title'] = '';
		
		$this->form_validation->set_rules('image_category_id',lang('category_image'),'required');
		if($this->form_validation->run())
		{
            $lb_title = $this->input->post('lb_title');
            $description = $this->input->post('description');
            $image_category_id = $this->input->post('image_category_id');
            
            if(!empty($_FILES['filedata']['tmp_name']))
            {         
                $cfFile=array();
                $cfFile['max_size']='5242880';//5MB
                //$cfFile['new_file_name']=time().'_'.rand(0,1000);
                //$cfFile['upload_dir']='./uploads/library/images';
                //$cfFile['flag_file'];
                $cfFile['base_upload_dir']='../uploads/images/';
                $cfFile['file']=$_FILES["filedata"];

                $obj_upload =  new $this->upload_library();
                $obj_upload->configUpload($cfFile);
                $filedata = $obj_upload->uploadFile();
      
                if($filedata['msg']=='')
                {
                    $data_file = array(
                      'lb_title'=>$lb_title,  
                      'image_category_id'=>$image_category_id,
                      'description'=>$description,
					  'lb_dir' => $filedata['str_dir'],
                      'lb_name' => $filedata['name'],
					  'lb_size' => $filedata['size'],
					  'lb_ext' => $filedata['ext'],
                      'admin_id'=>'1',
                      'dt_create'=>date('Y-m-d H:i:s')
					);
					$this->image->insert_file($data_file);
                    $data['file_name'] = $filedata['name'];
                    $data['str_dir']=$filedata['str_dir'];
                }else{
                    $data['err']=$filedata['msg'];
                }
            }else{
              $data['err'] = lang('fmng.please_choose_your_file');
            }
		}else{
            $this->pre_message = validation_errors();
        }
        $data['message'] = $this->pre_message;
      
      $data['list_category']=$this->image->getAllCategoryImage();
	  $this->_templates['page'] = 'image_manager_editor/upload';
      $this->site->load($this->_templates['page'],$data,'editor');
	}
}
?>